package com.zhongkai.listener;

import com.zhongkai.Bean.ContrastBean;
import com.zhongkai.Bean.UnitStartBean;
import com.zhongkai.MessageTool;
import com.zhongkai.app.service.DeviceOperationService;
import org.apache.log4j.Logger;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class ListenerConcurrentlySky implements MessageListenerConcurrently {

    private static final Logger logger = Logger.getLogger(ListenerConcurrentlySky.class);

    public static final ObjectMapper mapper = new ObjectMapper(); //解析json

    @Autowired
    private DeviceOperationService deviceOperationService;

/*
    @Autowired
    private GmsImService gmsImService;*/

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        try {
            Message msg = list.get(0);
            
            System.out.println("新消息到来");
            String body = new String(msg.getBody(), "utf-8");
            logger.info(msg.getTopic() + "-" + msg.getTags() + ":" + body);
            //  logger.info(msg.getKeys());
            String keys = msg.getTags();


            switch (keys) {
                case MessageTool.UNIT_START:    //控制设备

                    System.out.println(body);

                    System.out.println(deviceOperationService);
                    UnitStartBean unitStartBean = mapper.readValue(body, UnitStartBean.class);


                    /**
                     * 设备上线接口
                     *
                     * @param deviceID    设备uuid
                     * @param device_IP   设备ip
                     * @param set_status  设备状态 1 开机 2 关机 3离线
                     * @param is_warning  是否报警  1 正常 2 报警
                     * @param battery     总耗电量
                     * @param device_ver  版本号
                     * @param device_code 版本号标识
                     * */


                    deviceOperationService.deviceAdd(
                            unitStartBean.getUnit_uuid(),
                            unitStartBean.getUnit_ip(),
                            unitStartBean.getSet_status(),
                            unitStartBean.getIs_warning(),
                            unitStartBean.getAll_electricity(),
                            unitStartBean.getVersions(),
                            unitStartBean.getSim()
                    );
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;


                case MessageTool.HAND_REMOVED:

                    deviceOperationService.deviceDown(body);
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

                case MessageTool.UNIT_UPDATE:
                    //json = json.replace("Update:", "");
                    body = body.replace("Update:", "");
                    String[] parent = body.split(";");

                    try {
                        String contrast = ContrastBean.contrast(parent[0]); //0位状态码  1位设备uuid

                        deviceOperationService.deviceUpdate(parent[0], parent[1], contrast);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

/*                case MessageTool.WEN_KU:
                    //WEN_KU&uuid&昵称&ip

                    String[] split = body.split("&");

                    gmsImService.mmwenku(split[1],split[3]);

                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;*/
            }
        } catch (Exception e) {
            e.printStackTrace();
            //  logger.error("consumeMessage error, msg=" + msg.getTopic() + "-" + msg.getTags() + ":" + body, e);
            System.out.println("失败");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}

